/*
 * @Author: 蔡霸霸
 * @Date: 2020-05-19 15:35:57
 * @LastEditors: 蔡霸霸
 * @LastEditTime: 2020-06-03 11:55:02
 * @FilePath: \Nuxt_music\utils\time.js
 */ 
export function formatterTime(time, fmt) {
  //*转换时间戳
  //*示例 formatterTime(1565780223000 ,"yyyy-MM-dd hh:mm:ss")

  if (!time) {
    return ''
  }
  if (typeof time === 'object' || typeof time === 'OBJECT') {
    var z = {
      M: time.getMonth() + 1,
      d: time.getDate(),
      h: time.getHours(),
      m: time.getMinutes(),
      s: time.getSeconds()
    }
    fmt = fmt.replace(/(M+|d+|h+|m+|s+)/g, function(v) {
      return ((v.length > 1 ? '0' : '') + eval('z.' + v.slice(-1))).slice(-2)
    })
    return fmt.replace(/(y+)/g, function(v) {
      return time
        .getFullYear()
        .toString()
        .slice(-v.length)
    })
  } else if (typeof time === 'number') {
    const TIME = new Date(time)
    var z = {
      M: TIME.getMonth() + 1,
      d: TIME.getDate(),
      h: TIME.getHours(),
      m: TIME.getMinutes(),
      s: TIME.getSeconds()
    }
    fmt = fmt.replace(/(M+|d+|h+|m+|s+)/g, function(v) {
      return ((v.length > 1 ? '0' : '') + eval('z.' + v.slice(-1))).slice(-2)
    })
    return fmt.replace(/(y+)/g, function(v) {
      return TIME.getFullYear()
        .toString()
        .slice(-v.length)
    })
  } else return time
}
